let requestUrl = null;
let form = new FormData();
let typeArray = [null, "wq", "yj", "tk", "xl", "yd", "xz"];

/**
 * 添加
 */
function addHandler() {
    requestUrl = "/role_account/insert";

    $("#id").numberbox("readonly", false);

    $("#role_dialog").dialog("open");
}

/**
 * 修改
 */
function editHandler() {
    let rowData = $("#role_list").datagrid("getSelected");

    if (rowData) {
        requestUrl = "/role_account/updateById";

        $("#id").numberbox("readonly", true);
        $("#id").numberbox("setValue", rowData.id);

        $("#name").textbox("setValue", rowData.name);
        $("#gangId").combobox("setValue", rowData.gangId);
        $("#grade").numberspinner("setValue", rowData.grade);
        $("#score").numberspinner("setValue", rowData.score);
        $("#jinbi").numberspinner("setValue", rowData.jinbi);
        $("#schoolId").combobox("setValue", rowData.schoolId);
        $("#serverId").combobox("setValue", rowData.serverId);
        $("#birthday").datetimebox("setValue", rowData.birthday);
        $("#accountId").combobox("setValue", rowData.accountId);
        $("#categoryId").combobox("setValue", rowData.categoryId);

        $("#role_dialog").dialog("open");
    } else {
        alertMsg("请选择要修改的记录！", "warning");
    }
}

/**
 * 加点
 */
function jiadianHandler() {
    let rowData = $("#role_list").datagrid("getSelected");

    if (rowData) {
        let roleId = rowData.id;

        // 设置隐藏域角色ID的值
        $("#roleId").val(roleId);

        $("#jiadian_schema").combobox("reload", {
            roleId: roleId
        });

        // 查询角色当前开启的加点方案
        // 通过角色id和开启状态查询角色加点信息
        ajaxGet("/role_jiadian/selectJiadian", {
            roleId: rowData.id,
            openStatus: 1
        }, function(resp) {
            let response = resp.data;

            if (response) {
                // 设置加点方案为：加点方案一
                $("#jiadian_schema").combobox("setValue", response.jiadianSchema);

                $("#tz").numberspinner("setValue", response.tizhi);
                $("#ml").numberspinner("setValue", response.moli);
                $("#ll").numberspinner("setValue", response.liliang);
                $("#nl").numberspinner("setValue", response.naili);
                $("#mj").numberspinner("setValue", response.minjie);

                ajaxGet("/role_attribute/selectByRoleId", {
                    roleId: roleId
                }, function (resp) {
                    let data = resp.data;

                    if (data) {
                        $("#qx").numberspinner("setValue", data.qixue);
                        $("#mf").numberspinner("setValue", data.mofa);
                        $("#wlsh").numberspinner("setValue", data.wlsh);
                        $("#fssh").numberspinner("setValue", data.fssh);
                        $("#wlfy").numberspinner("setValue", data.wlfy);
                        $("#fsfy").numberspinner("setValue", data.fsfy);
                        $("#sd").numberspinner("setValue", data.sudu);
                    }
                }, error);
            }
        }, error);

        requestUrl = "/role_jiadian/update";

        $("#jiadian_dialog").dialog("open");
    } else {
        unselected();
    }
}

/**
 * 导入
 */
function importHandler() {
    requestUrl = "/role_account/importData";

    $("#file-name").empty();
    $("#file-size").empty();

    $("#import_dialog").dialog("open");
}

/**
 * 导出
 */
function exportHandler() {
    location.href = "/role_account/exportData";
}

/**
 * 批量删除
 */
function deleteHandler() {
    let rows = $("#role_list").datagrid("getChecked");
    let count = rows.length;

    if (count > 0) {
        confirmAction("确认删除勾选的" + count + "条数据？", function() {
            let ids = [];

            for (let i = 0; i < rows.length; i++) {
                ids.push(rows[i].id);
            }

            ajaxPost("/role_account/deleteByIds", {
                ids: JSON.stringify(ids)
            }, function(resp) {
                showMsg(resp.message);

                $("#role_list").datagrid("reload").datagrid("uncheckAll");
            }, error);
        });
    } else {
        alertMsg("请勾选要删除的角色！", "warning");
    }
}

/**
 * 预览角色
 */
function viewHandler() {
    let rowData = $("#role_list").datagrid("getSelected");

    if (rowData) {
        /**
         * west
         */

        ajaxGet("/role_category/selectById", {
            id: rowData.categoryId
        }, function(resp) {
            let data = resp.data;

            if (data) {
                $("#role_modeling").attr("src", data.image);
            }
        }, error);

        ajaxGet("/school/selectById", {
            id: rowData.schoolId
        }, function(resp) {
            let data = resp.data;

            $("#role_school").attr("src", data.image).attr("title", data.name);
        }, error);

        let roleId = rowData.id;

        $("#role_id").html(roleId);
        $("#role_name").html(rowData.name);
        $("#role_grade").html(rowData.grade);

        // 查询角色的图片
        ajaxGet("/qiling_taozhuang_image/selectImage", {
            roleId: rowData.categoryId
        }, function(resp) {
            let data = resp.data;

            if (data) {
                $("#role_picture").attr("src", data.image);
            }
        }, error);

        // 查询角色称谓
        ajaxGet("/role_chengwei/select", {
            useStatus: 1,
            roleId: roleId
        }, function(resp) {
            let data = resp.data;

            if (data) {
                ajaxGet("/chengwei/selectById", {
                    id: data.chengweiId
                }, function(resp) {
                    let data = resp.data;

                    if (data) {
                        $("#role_chengwei").html(data.name);

                        $("#chengwei_list").datalist("selectRecord", data.id);
                    }
                }, error);
            }
        }, error);

        /**
         * center
         */

        /**
         * 活力上限：500+角色等级*20，最高达到2600
         */
        let vitality = rowData.grade * 20 + 500;

        vitality = vitality > 2600 ? 2600 : vitality;

        // 设置愤怒
        let fn = $("#role_fennu");

        fn.progressbar("setValue", 89);
        fn.progressbar("setPercent", 89 / 150 * 100);

        // 设置活力
        let hl = $("#role_huoli");

        hl.progressbar("setValue", 1600);
        hl.progressbar("setText", "{value}/" + vitality);
        hl.progressbar("setPercent", 500 / vitality * 100);

        // 查询角色加点信息
        ajaxGet("/role_jiadian/selectJiadian", {
            unlockStatus: 1,
            openStatus: 1,
            roleId: roleId
        }, function(res) {
            let result = res.data;

            if (result) {
                ajaxGet("/role_attribute/selectByRoleId", {
                    roleId: roleId
                }, function (resp) {
                    let data = resp.data;

                    if (data) {
                        // 气血
                        let qx = $("#role_qixue");

                        qx.progressbar("setText", "{value}/" + data.qixue);
                        qx.progressbar("setValue", data.qixue);

                        // 魔法
                        let mf = $("#role_mofa");

                        mf.progressbar("setText", "{value}/" + data.mofa);
                        mf.progressbar("setValue", data.mofa);

                        // 物理伤害
                        $("#role_wlsh").html(data.wlsh);
                        // 物理防御
                        $("#role_wlfy").html(data.wlfy);
                        // 法术伤害
                        $("#role_fssh").html(data.fssh);
                        // 法术防御
                        $("#role_fsfy").html(data.fsfy);
                        //  速度
                        $("#role_sudu").html(data.sudu);
                        // 治疗强度
                        $("#role_zlqd").html(data.zlqd);
                    }
                }, error);
            }
        }, error);

        // 查询角色高级属性
        ajaxGet("/advanced_property/selectByRoleId", {
            roleId: roleId
        }, function(resp) {
            let data = resp.data;

            if (data) {
                $("#role_wlbj").html(data.wlbj);
                $("#role_wlkb").html(data.wlkb);
                $("#role_fsbj").html(data.fsbj);
                $("#role_fskb").html(data.fskb);
                $("#role_hswf").html(data.hswf);
                $("#role_hsff").html(data.hsff);
                $("#role_fymz").html(data.fymz);
                $("#role_dkfy").html(data.dkfy);
                $("#role_wbsh").html(data.wbsh);
                $("#role_fbsh").html(data.fbsh);
                $("#role_wldk").html(data.wldk);
                $("#role_fsdk").html(data.fsdk);
                $("#role_fslj").html(data.fslj);
                $("#role_hfqd").html(data.hfqd);
            }
        }, error);

        // 查询角色经验
        ajaxGet("/role_exp/selectByRoleId", {
            roleId: roleId
        }, function(res) {
            let data = res.data;

            if (data) {
                /*
                 * 角色当前的经验
                 */
                let currentExp = data.exp;
                // 盈福经验
                $("#role_yfjy").html(data.yfjy ? data.yfjy : 0);

                // 查询角色等级经验
                ajaxGet("/grade_exp/selectRoleExp", {
                    grade: rowData.grade + 1
                }, function (resp) {
                    let data = resp.data;

                    if (data) {
                        /*
                         * 角色当前等级升级所需的经验
                         */
                        let totalExp = data.exp;
                        let jy = $("#role_exp");

                        jy.progressbar("setValue", currentExp);
                        jy.progressbar("setText", "{value}/" + totalExp);
                        jy.progressbar("setPercent", currentExp / totalExp * 100);
                    }
                }, error);
            }
        }, error);

        $("#role_detail").dialog("open");
    } else {
        unselected();
    }
}

/**
 * 战前检查
 */
function checkHandler() {
    let rowData = $("#role_list").datagrid("getSelected");

    if (rowData) {
        let roleId = rowData.id;

        $("#config_role_id").val(roleId);

        $("#config_schema").combobox("reload", {
            roleId: roleId
        });
        $("#config_jiadian").combobox("reload", {
            roleId: roleId
        });
        $("#potency_fruit").combobox("reload", {
            roleId: roleId
        });
        $("#config_jingmai").combobox("reload", {
            roleId: roleId
        });
        $("#chongwu_name").combobox("reload", {
            roleId: roleId
        });

        // 查询角色开启的配置方案
        ajaxGet("/role_config_schema/selectOpenSchema", {
            roleId: roleId
        }, function (resp) {
            let data = resp.data;

            $("#config_schema").combobox("setValue", data.id);
        }, error);

        $("#config_dialog").dialog("open");
    } else {
        unselected();
    }
}

/**
 * 初始化技能
 */
function initHandler() {
    let rowData = $("#role_list").datagrid("getSelected");

    if (rowData) {
        $.messager.confirm("提示", "是否确认操作？", function(bool) {
            if (bool) {
                ajaxPost("/school_skill/init", {
                    roleId: rowData.id
                }, function(resp) {
                    showMsg(resp.message);
                }, error);
            }
        });
    } else {
        unselected();
    }
}

$(document).ready(function() {

    $("#id").numberbox({
        width: 110,
        required: true
    });

    $("#name").textbox({
        width: 110,
        required: true
    });

    $("#grade").numberspinner({
        min: 1,
        max: 115,
        width: 110,
        editable: true,
        required: true
    });

    $("#schoolId").combobox({
        url: "/school/selectAll",
        valueField: "id",
        textField: "name",
        width: 110,
        required: true,
        formatter: function(row) {
            return "<img height='20' src='" + row.image + "' />&nbsp;" + row.name;
        },
        onSelect: function(record) {
            if(record) {
                let params = {
                    phyle: record.phyle
                };

                if (record.gender) {
                    params.gender = record.gender;
                }

                $("#categoryId").combobox("reload", params);
            }
        }
    });

    $("#categoryId").combobox({
        url: "/role_category/selectList",
        valueField: "id",
        textField: "name",
        width: 110,
        editable: true,
        required: true,
        groupField: "phyle",
        groupFormatter: function(group) {
            ajaxGet("/phyle/selectById", {
                id: group
            }, function (resp) {
                let data = resp.data;

                $("#phyle_" + group).html(data.name);
            }, error);

            return "<span class='blue' id='phyle_" + group + "'></span>";
        },
        formatter: function(row) {
            return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
        }
    });

    $("#gangId").combobox({
        url: "/gang/selectAll",
        valueField: "id",
        textField: "name",
        width: 110
    });

    $("#score").numberspinner({
        min: 0,
        width: 110,
        required: true
    });

    $("#jinbi").numberspinner({
        width: 110,
        required: true
    });

    $("#areaId").combobox({
        url: "/area/selectAll",
        valueField: "id",
        textField: "name",
        width: 110,
        prompt: "--大区--",
        onSelect: function(record) {
            if(record) {
                $("#serverId").combobox("reload", {
                    areaId: record.id
                });
            }
        }
    });

    $("#serverId").combobox({
        url: "/server/selectByAreaId",
        valueField: "id",
        textField: "name",
        width: 110,
        required: true,
        prompt: "--服务器--"
    });

    $("#accountId").combobox({
        url: "/account/selectAll",
        valueField: "id",
        textField: "account",
        width: 225,
        required: true
    });

    $("#birthday").datetimebox({
        width: 176,
        required: true,
        showSeconds: true
    });

    $("#role_dialog").dialog({
        title: "角色信息",
        width: 600,
        buttons: [{
            iconCls: "icon-save",
            text: "保存",
            handler: function() {
                let selector = "#role_form";

                checkForm(selector, function () {
                    let data = $(selector).serialize();

                    ajaxPost(requestUrl, data, function(resp) {
                        showMsg(resp.message);

                        // 清空表单
                        $(selector).form("clear");
                        // 关闭对话框
                        $("#role_dialog").dialog("close");
                        // 刷新角色列表
                        $("#role_list").datagrid("reload");
                        // 重新加载角色造型下拉框的数据
                        $("#categoryId").combobox("reload", {});
                    }, error);
                });
            }
        }, {
            iconCls: "icon-cancel",
            text: "取消",
            handler: function() {
                // 清空表单
                $("#role_form").form("clear");
                // 关闭对话框
                $("#role_dialog").dialog("close");
                // 重新加载角色造型下拉框的数据
                $("#categoryId").combobox("reload", {});
            }
        }]
    });

    /******************************************************************************************************/

    /**
     *	jiadian_dialog
     */
    $("#jiadian_schema").combobox({
        url: "/jiadian_schema/selectByRoleId",
        valueField: "id",
        textField: "name",
        width: 110,
        editable: false,
        onSelect: function(record) {
            let rowData = $("#role_list").datagrid("getSelected");
            let jiadianSchema = record.id;
            let roleId = rowData.id;

            /*
             * 通过角色ID和加点方案查询角色加点
             */
            ajaxGet("/role_jiadian/selectJiadian", {
                roleId: roleId,
                jiadianSchema: jiadianSchema
            }, function(resp) {
                let data = resp.data;

                if (data) {
                    // 没有到解锁该加点方案
                    if (data.unlockStatus === 0) {
                        // 可以花费金币提前解锁第3、4套加点方案
                        $.messager.confirm("提示", record.unlockTip, function (bool) {
                            if (bool) {
                                ajaxPost("/role_jiadian/unlock", {
                                    roleId: roleId,
                                    jiadianSchema: jiadianSchema
                                }, function (resp) {
                                    showMsg(resp.message);

                                    $("#jiadian_schema").combobox("reload", {
                                        roleId: roleId
                                    }).combobox("setValue", jiadianSchema);
                                }, function (resp) {
                                    error(resp);

                                    $("#jiadian_schema").combobox("setValue", 1);
                                });
                            }
                        });
                    } else {
                        if (data.openStatus === 0) {
                            $("#opened").linkbutton({
                                text: "&emsp;&emsp;开启&emsp;&emsp;"
                            }).linkbutton("enable");
                        } else {
                            $("#opened").linkbutton({
                                text: "当前方案已开启"
                            }).linkbutton("disable");
                        }

                        $("#tz").numberspinner("setValue", data.tizhi);
                        $("#ml").numberspinner("setValue", data.moli);
                        $("#ll").numberspinner("setValue", data.liliang);
                        $("#nl").numberspinner("setValue", data.naili);
                        $("#mj").numberspinner("setValue", data.minjie);

                        // 通过ID查询角色属性
                        ajaxGet("/role_attribute/selectByRoleId", {
                            roleId: data.roleId
                        }, function (resp) {
                            let data = resp.data;

                            if (data) {
                                $("#qx").numberspinner("setValue", data.qixue);
                                $("#mf").numberspinner("setValue", data.mofa);
                                $("#wlsh").numberspinner("setValue", data.wlsh);
                                $("#fssh").numberspinner("setValue", data.fssh);
                                $("#wlfy").numberspinner("setValue", data.wlfy);
                                $("#fsfy").numberspinner("setValue", data.fsfy);
                                $("#sd").numberspinner("setValue", data.sudu);
                            }
                        }, error);
                    }
                }
            });
        }
    });

    $("#edit").linkbutton({
        iconCls: "icon-edit"
    }).click(function () {
        let jiadianSchema = $("#jiadian_schema").combobox("getValue");

        if (jiadianSchema) {
            $.messager.prompt("改名", "输入新的加点方案名字（最多6个字）", function (value) {
                if (value) {
                    let rowData = $("#role_list").datagrid("getSelected");
                    let roleId = rowData.id;

                    ajaxPost("/role_jiadian/rename", {
                        name: value,
                        roleId: roleId,
                        jiadianSchema: jiadianSchema
                    }, function (resp) {
                        showMsg(resp.message);

                        $("#jiadian_schema").combobox("reload", {
                            roleId: roleId
                        });
                    }, error);
                }
            });
        } else {
            alertMsg("请选择加点方案");
        }
    });

    $("#opened").linkbutton({
        text: "&emsp;&emsp;开启&emsp;&emsp;"
    }).click(function () {
        // 获取选择的加点方案
        let jiadianSchema = $("#jiadian_schema").combobox("getValue");
        // 获取选中的角色
        let rowData = $("#role_list").datagrid("getSelected");
        let roleId = rowData.id;

        // 通过角色ID和加点方案查询角色加点
        ajaxGet("/role_jiadian/selectJiadian", {
            roleId: roleId,
            jiadianSchema: jiadianSchema
        }, function(resp) {
            let data = resp.data;

            if (data && data.openStatus === 0) {
                // 开启加点
                ajaxPost("/role_jiadian/enable", {
                    roleId: roleId,
                    jiadianSchema: jiadianSchema
                }, function(resp) {
                    showMsg(resp.message);

                    $("#opened").linkbutton({
                        text: "当前方案已开启"
                    });

                    $('#opened').linkbutton("disable");
                }, error);
            }
        }, error);
    });

    $("#qx").numberspinner({
        min: 0,
        width: 100,
        required: true
    });

    $("#mf").numberspinner({
        min: 0,
        width: 100,
        required: true
    });

    $("#wlsh").numberspinner({
        min: 0,
        width: 100,
        required: true
    });

    $("#fssh").numberspinner({
        min: 0,
        width: 100,
        required: true
    });

    $("#wlfy").numberspinner({
        min: 0,
        width: 100,
        required: true
    });

    $("#fsfy").numberspinner({
        min: 0,
        width: 100,
        required: true
    });

    $("#sd").numberspinner({
        min: 0,
        width: 100,
        required: true
    });

    $("#increment").linkbutton({
        text: "增加属性"
    }).click(function() {
        unopen();
    });

    $("#recommend").linkbutton({
        text: "推荐加点"
    }).click(function() {
        unopen();
    });

    $("#simulate").linkbutton({
        text: "属性模拟"
    }).click(function() {
        unopen();
    });

    $("#tz").numberspinner({
        min: 0,
        width: 220,
        required: true
    });

    $("#ml").numberspinner({
        min: 0,
        width: 220,
        required: true
    });

    $("#ll").numberspinner({
        min: 0,
        width: 220,
        required: true
    });

    $("#nl").numberspinner({
        min: 0,
        width: 220,
        required: true
    });

    $("#mj").numberspinner({
        min: 0,
        width: 220,
        required: true
    });

    $("#wash").linkbutton({
        text: "&emsp;&emsp;洗点&emsp;&emsp;"
    });

    $("#save").linkbutton({
        text: "&emsp;确认加点&emsp;"
    }).click(function() {
        let selector = "#jiadian_form";

        checkForm(selector, function () {
            let data = $(selector).serialize();

            ajaxPost(requestUrl, data, function(resp) {
                showMsg(resp.message);

                let rowData = $("#role_list").datagrid("getSelected");

                // 只有选中角色点击加点才会设置角色ID
                if (rowData) {
                    // 防止清空表单后角色ID为空的情况
                    $("#roleId").val(rowData.id);
                }

                $(selector).form("clear");
                $("#jiadian_dialog").dialog("close");
            }, error);
        });
    });

    $("#jiadian_dialog").dialog({
        title: "人物加点"
    });

    /******************************************************************************************************/

    /**
     * role_detail
     */
    $("#role_qixue").progressbar({
        width: 225,
        text: "{value}"
    }).progressbar("setColor", "#B22222DD");

    $("#role_mofa").progressbar({
        width: 225,
        text: "{value}"
    }).progressbar("setColor", "#6495ED");

    $("#role_fennu").progressbar({
        width: 225,
        text: "{value}/150"
    }).progressbar("setColor", "#CD8A32E3");

    $("#role_huoli").progressbar({
        width: 175
    }).progressbar("setColor", "#c864ed");

    $("#use").on("click", function () {
        unopen();
    });

    $("#menu").tabs({
        fit: true,
        pill: true,
        narrow: false,
        border: false,
        justified: true,
        closable: false,
        showHeader: true
    });

    $("#role_exp").progressbar({
        width: 190,
        text: "{value}/43527489"
    }).progressbar("setColor", "#32CD32");

    $("#tips").linkbutton({
        iconCls: "icon-help"
    }).click(function() {
        $("#about").dialog("open");
    });

    $(".property_panel").panel({
        fit: true
    });

    $("#role_detail").dialog({
        title: "人物属性"
    });

    $("#about").dialog({
        title: "经验系统",
        width: 405
    });

    $("#chengwei_list").datalist({
        url: "/chengwei/selectByRoleId",
        width: 207,
        height: 298,
        idField: "id",
        valueField: "id",
        textField: "name",
        textFormatter: function(value, rowData, rowIndex) {
            let text = "";

            text += "<div style='width:178px;height:50px;'>";
            text += 	"<span style='font-size:20px;line-height:50px;'>";
            text += 		rowData.name;
            text += 	"</span>";
            text += "</div>";

            return text;
        },
        onSelect: function(rowIndex, rowData) {
            if (rowData) {
                $("#note").html(rowData.note);
                $("#get").html(rowData.get);

                // 限时称谓
                if (rowData.permanent === 0) {
                    let role = $("#role_list").datagrid("getSelected");

                    if (role) {
                        ajaxGet("/role_chengwei/select", {
                            roleId: role.id,
                            chengweiId: rowData.id
                        }, function (resp) {
                            let data = resp.data;

                            if (data) {
                                let endTime = data.endTime;

                                if (endTime) {
                                    $("#end_time").html(endTime);
                                    $("#deadline").show();
                                }
                            }
                        }, error);
                    }
                } else {
                    $("#deadline").hide();
                }
            }
        }
    });

    $("#switch").switchbutton({
        width: 58,
        height: 16
    });

    $("#change").linkbutton({
        iconCls: "icon-upload"
    }).click(function() {
        let rowData = $("#role_list").datagrid("getSelected");

        if (rowData) {
            ajaxGet("/role_category/selectById", {
                id: rowData.categoryId
            }, function(resp) {
                let data = resp.data;

                $("#role_image").attr("src", data.image);
            }, error);

            $("#chengwei_list").datalist("reload", {
                roleId: rowData.id
            });
            $("#chengwei_dialog").dialog("open");
        } else {
            alertMsg("请选择一个角色！");
        }
    });

    $("#confirm").linkbutton({
        text: "&emsp;&emsp;确&emsp;认&emsp;&emsp;"
    }).click(function() {
        let rowData = $("#role_list").datagrid("getSelected");

        if (rowData) {
            let chengwei = $("#chengwei_list").datalist("getSelected");

            if (chengwei) {
                ajaxPost("/role_chengwei/change", {
                    chengweiId: chengwei.id,
                    roleId: rowData.id
                }, function(resp) {
                    showMsg(resp.message);

                    ajaxGet("/role_chengwei/select", {
                        useStatus: 1,
                        roleId: rowData.id
                    }, function(resp) {
                        let data = resp.data;

                        if (data) {
                            ajaxGet("/chengwei/selectById", {
                                id: data.chengweiId
                            }, function(res) {
                                let result = res.data;

                                $("#chengwei").html(result.name);
                                $("#chengwei_dialog").dialog("close");
                            }, error);
                        }
                    }, error);
                }, error);
            } else {
                alertMsg("请选择一个称谓！", "info");
            }
        } else {
            alertMsg("请选择一个角色！", "info");
        }
    });

    $("#chengwei_dialog").dialog({
        title: "角色称谓"
    });

    /******************************************************************************************************/

    $("#select_file").filebox({
        buttonText: "选择文件",
        width: 200,
        required: true,
        onChange: function() {
            let file = $(this).context.ownerDocument.activeElement.files[0];

            form.append("file", file);

            $("#file-name").html(file.name);
            $("#file-size").html((file.size / 1024).toFixed(1) + "KB");
        }
    })

    $("#import_dialog").dialog({
        title: "数据导入",
        buttons: [{
            iconCls: "icon-ok",
            text: "导入",
            handler: function() {
                let bool = $("#import_form").form("validate");

                if (bool) {
                    postForm(requestUrl, form, function (resp) {
                        showMsg(resp.message);

                        $("#import_dialog").dialog("close");
                        $("#member_list").datagrid("reload");
                    }, error);
                } else {
                    alertMsg("请选择文件", "warning");
                }
            }
        }, {
            iconCls: "icon-cancel",
            text: "取消",
            handler: function() {
                $("#import_dialog").dialog("close");

                resetValue("#select_file");

                form.delete("file");
            }
        }]
    });

    /******************************************************************************************************/

    $("#config_dialog .splitter").textbox({
        width: 300
    });

    $("#config_dialog .help").linkbutton({
        iconCls: "icon-help"
    });

    $("#config_schema").combobox({
        url: "/role_config_schema/selectByRoleId",
        valueField: "id",
        textField: "name",
        width: 120,
        required: false,
        onSelect: function (record) {
            if (record) {
                let configId = record.id;

                // 查询角色配置-加点
                ajaxGet("/role_config_jiadian/selectByConfigId", {
                    configId: configId
                }, function (resp) {
                    let data = resp.data;

                    $("#config_jiadian").combobox("setValue", data.jiadianId);
                }, error);

                // 查询角色配置-经脉
                ajaxGet("/role_config_jingmai/selectByConfigId", {
                    configId: configId
                }, function (resp) {
                    let data = resp.data;

                    $("#config_jingmai").combobox("setValue", data.jingmaiId);
                }, error);

                // 查询角色配置-潜能
                ajaxGet("/role_config_potency/selectByConfigId", {
                    configId: configId
                }, function (resp) {
                    let data = resp.data;

                    $("#potency_fruit").combobox("setValue", data.potencyId);
                }, error);

                // 查询角色配置-装备
                ajaxGet("/role_config_equipment/selectByConfigId", {
                    configId: configId
                }, function (resp) {
                    let data = resp.data;

                    ajaxGet("/equipment_schema/selectById", {
                        id: data.equipmentId
                    }, function (resp) {
                        let data = resp.data;

                        // $("#equpment_menu").append("<div>" + data.name + "</div>");

                        // 查询角色穿戴的所有装备信息
                        ajaxGet("/schema_equipment/selectBySchemaId", {
                            schmaId: data.id
                        }, function (resp) {
                            let list = resp.data;

                            for (let i = 0; i < list.length; i++) {
                                let equipment = list[i];

                                $("#config_" + typeArray[equipment.type]).attr({
                                    src: equipment.image,
                                    title: equipment.name + "：" + equipment.note
                                });
                            }
                        }, error);
                    }, error);
                }, error);

                // 查询角色配置-法宝
                ajaxGet("/role_config_fabao/selectByConfigId", {
                    configId: configId
                }, function (resp) {
                    let list = resp.data;

                    for (let i = 0; i < list.length; i++) {
                        let fabao = list[i];

                        $("#fabao_" + (i + 1)).attr({
                            src: fabao.image,
                            title: fabao.name + "：" + fabao.note
                        });
                    }
                }, error);

                // 查询角色配置-宠物
                ajaxGet("/role_config_chongwu/selectByConfigId", {
                    configId: configId
                }, function (resp) {
                    let data = resp.data;
                    let chongwuId = data.chongwuId;

                    // 通过宠物ID查询坐骑宠物
                    ajaxGet("/zuoqi_skill/selectByChongwuId", {
                        chongwuId: chongwuId
                    }, function(resp) {
                        let data = resp.data;

                        $("#config_chongwu").attr({
                            alt: data.name,
                            src: data.image,
                            title: data.name
                        })
                    }, error);

                    $("#chongwu_name").combobox("setValue", data.chongwuId);
                }, error);
            }
        }
    });

    $("#config_jiadian").combobox({
        url: "/role_jiadian/selectByRoleId",
        valueField: "id",
        textField: "name",
        width: 100,
        editable: false,
        onSelect: function (record) {
            if (record) {
                // 查询角色属性
                ajaxGet("/role_attribute/selectByRoleIdAndJiadianId", {
                    roleId: record.roleId,
                    jiadianId: record.id
                }, function (resp) {
                    let data = resp.data;

                    $("#config_qx").html(data.qixue);
                    $("#config_mf").html(data.mofa);
                    $("#config_wlsh").html(data.wlsh);
                    $("#config_fssh").html(data.fssh);
                    $("#config_wlfy").html(data.wlfy);
                    $("#config_fsfy").html(data.fsfy);
                    $("#config_sd").html(data.sudu);
                    $("#config_zlqd").html(data.zlqd);

                    ajaxGet("/advanced_property/selectByRoleId", {
                        roleId: record.roleId,
                        jiadianId: record.id
                    }, function (resp) {
                        let data = resp.data;

                        $("#config_dkfy").html(data.dkfy);
                        $("#config_fymz").html(data.fymz);
                    }, error);
                }, error);
            }
        }
    });

    $("#jiadian_edit").linkbutton({
        iconCls: "icon-edit"
    });

    $("#change_body").combobox({
        width: 100
    });

    $("#body_edit").linkbutton({
        iconCls: "icon-edit"
    });

    $("#potency_fruit").combobox({
        url: "/potency_fruit_schema/selectByRoleId",
        valueField: "id",
        textField: "name",
        width: 100,
        editable: false
    });

    $("#fruit_edit").linkbutton({
        iconCls: "icon-edit"
    });

    $("#config_jingmai").combobox({
        url: "/jingmai_schema/selectByRoleId",
        valueField: "id",
        textField: "name",
        width: 100,
        editable: false
    });

    $("#jingmai_edit").linkbutton({
        iconCls: "icon-edit"
    });

    $("#fabao_edit").linkbutton({
        iconCls: "icon-edit"
    });

    $("#chongwu_name").combobox({
        url: "/chongwu/selectByRoleId",
        valueField: "id",
        textField: "name",
        width: 150,
        editable: false,
        formatter: function(row) {
            ajaxGet("/chongwu_category/selectById", {
                id: row.categoryId
            }, function(resp) {
                let data = resp.data;

                $("#chongwu_" + row.id).attr({
                    "title": data.name,
                    "src": data.image,
                    "alt": data.name
                });
            }, error);

            return "<img height='20' id='chongwu_" + row.id + "' />&ensp;" + row.name;
        }
    });

    $("#chongwu_edit").linkbutton({
        iconCls: "icon-edit"
    });

    $("#chage_equipment").linkbutton({
        text: "切换",
        iconCls: "icon-reload"
    }).on("click", function () {
        unopen();
    });

    $("#edit_equipment").linkbutton({
        text: "编辑",
        iconCls: "icon-edit"
    }).on("click", function () {
        unopen();
    });

    $("#save_config").linkbutton({
        text: "保存",
        iconCls: "icon-save"
    }).on("click", function () {
        let data = $("#config_form").serialize();

        ajaxPost("/role_config_schema/save", data,function (resp) {
            alertMsg(resp.message);
        }, error);
    });

    $("#config_dialog").dialog({
        title: "战前检查",
        closable: true
    });

    /******************************************************************************************************/

    /**
     * 表格体的高度
     * @type {number}
     */
    let heigt_body = heigt_tr_with_image * 10;
    /**
     * 表格高度
     * @type {number}
     */
    let height_table = heigt_tool + heigt_header + heigt_filter + heigt_body + heigt_footer + heigt_scoll_bar + 2;

    let datagrid = $("#role_list").datagrid({
        url: "/role_account/selectByPage",
        method: "post",
        height: height_table,
        rownumbers: true,
        pagination: true,
        remoteFilter: true,
        clientPaging: false,
        onHeaderContextMenu: function(e) {
            e.preventDefault();

            if (!columnMenu){
                createColumnMenu("#role_list");
            }
            columnMenu.menu("show", {
                left: e.pageX,
                top: e.pageY
            });
        },
        onLoadSuccess: function () {
            let selector = "#role_list";
            let fields = getFields(selector);

            if (fields.length > 0) {
                let datagrid = $(selector);

                for (let i = 0; i < fields.length; i++) {
                    let field = fields[i];

                    // 隐藏未选中的字段
                    if (!field.selected) {
                        datagrid.datagrid("hideColumn", field.name);
                    }
                }

                datagrid.datagrid("fitColumns");
            }
        },
        toolbar: [{
            iconCls: "icon-add",
            text: "添加",
            handler: function() {
                addHandler();
            }
        }, "-", {
            iconCls: "icon-edit",
            text: "修改",
            handler: function() {
                editHandler();
            }
        }, "-", {
            iconCls: "icon-set",
            text: "加点",
            handler: function() {
                jiadianHandler();
            }
        }, "-", {
            iconCls: "icon-upload",
            text: "导入",
            handler: function() {
                importHandler();
            }
        }, "-", {
            iconCls: "icon-download",
            text: "导出",
            handler: function() {
                exportHandler();
            }
        }, "-", {
            iconCls: "icon-delete",
            text: "删除",
            handler: function() {
                deleteHandler();
            }
        }, "-", {
            iconCls: "icon-search",
            text: "预览角色",
            handler: function() {
                viewHandler();
            }
        }, "-", {
            iconCls: "icon-ok",
            text: "战前检查",
            handler: function() {
                checkHandler()
            }
        }, "-", {
            iconCls: "icon-reload",
            text: "初始化技能",
            handler: function() {
                initHandler()
            }
        }],
        frozenColumns: [[
            {field: "ck", checkbox: true},
            {field: "id", title: "角色ID", align: "center", sortable: true, width: 150},
            {field: "name", title: "角色名", align: "center", width: 150},
            {field: "categoryId", title: "角色造型", align: "center", width: 150
                , formatter: function(value, rowData, rowIndex) {
                    ajaxGet("/role_category/selectById", {
                        id: value
                    }, function(resp) {
                        let data = resp.data;

                        $("#role_category_" + rowIndex).attr({
                            title: data.name,
                            alt: data.name,
                            src: data.image
                        });
                    }, error);

                    return "<img alt='暂无图片' height='" + size + "' id='role_category_" + rowIndex + "' />";
                }
            }
        ]],
        columns: [[
            {field: "grade", title: "角色等级", align: "center", sortable: true, width: 150
                , formatter: function(value) {
                    let group; // 组别

                    if(value >= 0 && value <= 69) { // [0, 69]精锐组
                        group = "精锐组";
                    } else if(value >= 70 && value <= 89) { // [70, 89]勇武组
                        group = "勇武组";
                    } else if(value >= 90 && value <= 115) { // [90, 115]神威组
                        group = "神威组";
                    } else { // [-∞, 0) ∪ (115, +∞)
                        group = "等级不合法";
                    }

                    return "<span title='" + group + "'>" + value+ "级</span>";
                }
            },
            {field: "score", title: "人物评分", align: "center", sortable: true, width: 150},
            {field: "birthday", title: "角色创建时间", align: "center", sortable: true, width: 200},
            {field: "jinbi", title: "金币数量", align: "center", sortable: true, width: 150},
            {field: "schoolId", title: "角色门派", align: "center", width: 150
                , formatter: function(value, rowData, rowIndex) {
                    ajaxGet("/school/selectById", {
                        id: value
                    }, function(resp) {
                        let data = resp.data;

                        $("#school_" + rowIndex).attr({
                            title: data.name,
                            alt: data.name,
                            src: data.image
                        });
                    }, error);

                    return "<img alt='暂无图片' height='" + size + "' id='school_" + rowIndex + "' />";
                }
            },
            {field: "serverId", title: "服务器", align: "center", width: 150
                , formatter: function(value, rowData, rowIndex) {
                    if (value) {
                        ajaxGet("/server/selectById", {
                            id: value
                        }, function (resp) {
                            let server = resp.data;

                            ajaxGet("/area/selectById", {
                                id: server.areaId
                            }, function (resp) {
                                let area = resp.data;

                                $("#server_" + rowIndex).html(area.name + "-" + server.name);
                            }, error);
                        }, error);

                        return "<div id='server_" + rowIndex + "'></div>";
                    } else {
                        return "<div>/</div>";
                    }
                }
            },
            {field: "accountId", title: "所属账号", align: "center", width: 250
                , formatter: function(value, rowData, rowIndex) {
                    ajaxGet("/account/selectById", {
                        id: value
                    }, function(resp) {
                        let data = resp.data;

                        $("#account_" + rowIndex).html(data.account);
                    }, error);

                    return "<div id='account_" + rowIndex + "'></div>";
                }
            },
            {field: "lastUpdateTime", title: "最后一次修改时间", align: "center", sortable: true, width: 200
                , formatter: function(value) {
                    if (value) {
                        return "<div>" + value + "</div>";
                    }

                    return "/";
                }
            }
        ]]
    });

    datagrid.datagrid("enableFilter", [{
        field: "id",
        type: "textbox",
        operator: ["equal"]
    }, {
        field: "name",
        type: "textbox",
        operator: ["equal", "contains"]
    }, {
        field: "grade",
        type: "numberspinner",
        operator: ["equal", "less", "lessorequal", "greater", "greaterorequal"],
        options: {
            min: 1,
            max: 115
        }
    }, {
        field: "jinbi",
        type: "numberspinner",
        operator: ["equal", "less", "lessorequal", "greater", "greaterorequal"],
    }, {
        field: "score",
        type: "numberspinner",
        operator: ["equal", "less", "lessorequal", "greater", "greaterorequal"],
        options: {
            min: 1
        }
    }, {
        field: "schoolId",
        type: "combobox",
        options: {
            url: "/school/selectAll",
            valueField: "id",
            textField: "name",
            panelHeight: "auto",
            formatter: function(row) {
                return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
            }
        },
        operator: ["equal"]
    }, {
        field: "categoryId",
        type: "combobox",
        options: {
            url: "/role_category/selectAll",
            valueField: "id",
            textField: "name",
            panelHeight: "auto",
            groupField: "phyle",
            groupFormatter: function(group) {
                ajaxGet("/phyle/selectById", {
                    id: group
                }, function (resp) {
                    let data = resp.data;

                    $("#role_phyle_" + group).html(data.name);
                }, error);

                return "<span class='blue' id='role_phyle_" + group + "'></span>";
            },
            formatter: function(row) {
                return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
            }
        },
        operator: ["equal"]
    }, {
        field: "accountId",
        type: "combobox",
        options: {
            url: "/account/selectAll",
            valueField: "id",
            textField: "account",
            panelHeight: "auto",
        },
        operator: ["equal"]
    }]);

});